$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'psorder/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 50 ,hidden:true},
            { label: '订单号', name: 'ordernum', index: 'ordernum', width: 150 },
            { label: '分公司名称', name: 'branchCompany', index: 'branch_company', width: 180 },
            { label: '网点id', name: 'netPointId', index: 'net_point_id', width: 50 ,hidden:true},
            { label: '网点名称', name: 'netPoint', index: 'net_point', width: 130 },
            { label: '接单司机id', name: 'driverId', index: 'driver_id', width: 80 ,hidden:true},
            { label: '接单司机姓名', name: 'driverName', index: 'driver_name', width: 80 },
            { label: '接单司机电话', name: 'driverTel', index: 'driver_tel', width: 100 },
			{ label: '用户id', name: 'userId', index: 'user_id', width: 80 ,hidden:true},
            { label: '用户姓名', name: 'userName', index: 'user_name', width: 80 },
			{ label: '用户电话', name: 'userPhone', index: 'user_phone', width: 100 },
			{ label: '下单时间', name: 'createTime', index: 'create_time', width: 120 },
			{ label: '接收订单时间', name: 'receiveTime', index: 'receive_time', width: 80 , hidden:true},
			{ label: '完成订单时间', name: 'completeTime', index: 'complete_time', width: 80 , hidden:true},
			{ label: '起点名称', name: 'startName', index: 'start_name', width: 170 },
			{ label: '起点经度', name: 'startLng', index: 'start_lng', width: 80 ,hidden:true},
			{ label: '起点纬度', name: 'startLat', index: 'start_lat', width: 80 ,hidden:true},
			{ label: '终点名称', name: 'endName', index: 'end_name', width: 170 },
			{ label: '终点经度', name: 'endLng', index: 'end_lng', width: 80,hidden:true },
			{ label: '终点纬度', name: 'endLat', index: 'end_lat', width: 80,hidden:true },
			{ label: '当前地址名称', name: 'curAddress', index: 'cur_address', width: 80,hidden:true },
			{ label: '当前经度', name: 'curLng', index: 'cur_lng', width: 80 ,hidden:true},
			{ label: '当前纬度', name: 'curLat', index: 'cur_lat', width: 80 ,hidden:true},
			{ label: '行车距离(理论)', name: 'distance', index: 'distance', width: 80 , hidden:true},
			{ label: '实际行驶距离', name: 'realdistance', index: 'realdistance', width: 80 , hidden:true},
			{ label: '备注', name: 'note', index: 'note', width: 80 ,hidden:true},
			{ label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80,
                formatter:function(v){
                    if(v==0){
                        return "未接单";
                    }else if(v==1){
                        return "到达目的地";
                    }else if(v==2){
                        return "已支付";
                    }else if(v==3){
                        return "已取消(用户)";
                    }else if(v==4){
                        return "已取消(司机)";
                    }else if(v==5){
                        return "到达发货地";
                    }else if(v==6){
                        return "已出发";
                    }else if(v==7){
                        return "卸货完成";
                    }else if(v==8){
                        return "代收费";
                    }else if(v==9){
                        return "已接单";
                    }else{
                    	return "";
					}
                }
			},
			{ label: '订单金额', name: 'orderMoney', index: 'order_money', width: 80 },
			{ label: '支付方式', name: 'payType', index: 'pay_type', width: 80,
                formatter:function(v){
                    if(v=='wxpay'){
                        return "微信";
                    }else if(v=='alipay'){
                        return "支付宝";
                    }else if(v=='cash'){
                        return "现金";
                    }else{
                    	return "";
					}
                }

			},
			{ label: '支付时间', name: 'payTime', index: 'pay_time', width: 120 },
			{ label: '实际支付金额', name: 'payRealymoney', index: 'pay_realymoney', width: 100 },
			{ label: '额外费用', name: 'extraCharge', index: 'extra_charge', width: 80 }, 			
			{ label: '额外费用说明', name: 'extraChargeNote', index: 'extra_charge_note', width: 80, hidden:true },
			{ label: '支付返回的订单号', name: 'payOrder', index: 'pay_order', width: 80, hidden:true },
			{ label: '支付状态', name: 'payStatus', index: 'pay_status', width: 80,
                formatter:function(v){
                    if(v==0){
                        return "未完成";
                    }else if(v==1){
                        return "已完成";
                    }else if(v==2){
                        return "已支付";
                    }else if(v==3){
                        return "已取消";
                    }else{
                    	return "";
					}
                }
			},
            { label: '操作', width: 80 ,formatter: function(value, options, row){
                return '<input type="button" class="btn btn-primary" onclick="vm.update('+row.id+')" value="详情"/>';
            }}
        ],
		viewrecords: true,
        height: 680,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	// $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getBranchCompanyList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            branchCompany:null,
            netPoint:null,
            startCreateTime:null,
            endCreateTime:null,
            driverName:null,
            userName:null
        },
        showList: true,
        title: null,
        psOrder: {
            branchCompany:null,
            branchCompanyId:null,
            netPoint:null,
            netPointId:null

        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
        empty: function () {
            vm.psOrder.branchCompany = null;
            vm.psOrder.branchCompanyId = null;
            vm.psOrder.netPoint = null;
            vm.psOrder.netPointId = null;
            vm.q.branchCompany = null;
            vm.q.netPoint = null;
			vm.q.startCreateTime = null,
			vm.q.endCreateTime = null,
		    vm.q.driverName = null,
			vm.q.userName = null
            vm.reload();
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.psOrder = {};
		},
		update: function (id) {
			vm.showList = false;
            vm.title = "订单详情";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.psOrder.id == null ? "psorder/save" : "psorder/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.psOrder),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "psorder/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "psorder/info/"+id, function(r){
                vm.psOrder = r.psOrder;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'branchCompany': vm.q.branchCompany, 'netPoint': vm.q.netPoint, 'startCreateTime': dateChange(vm.q.startCreateTime),
					'endCreateTime': dateChange(vm.q.endCreateTime), 'driverName': vm.q.driverName, 'userName': vm.q.userName
                },
                page:page
            }).trigger("reloadGrid");
		},
        BranchCompanySelect:function(){
            utils.openDialogView('分公司列表',$("#windowBranchCompany"),'900px','500px',1);
        },
        getBranchCompanyList: function(){
            $("#jqGridBranchCompany").jqGrid({
                url: baseURL + 'psbranchcompany/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
                    { label: '分公司名称', name: 'companyName', index: 'company_name', width: 100 },
                    { label: '分公司营业执照', name: 'companyLicenceImg', index: 'company_licence_img', width: 80 ,hidden:true},
                    { label: '分公司地址', name: 'address', index: 'address', width: 150 },
                    { label: '联系电话', name: 'contactNumber', index: 'contact_number', width: 80 },
                    { label: '联系人', name: 'contactPeople', index: 'contact_people', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 85,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerBranchCompany",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridBranchCompany").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        BranchPointSelect:function(){
            if( vm.psOrder.branchCompanyId == null ) {
                alert("请先选择一个分公司!");
                return ;
            }
            utils.openDialogView('网点列表',$("#windowNetPoint"),'900px','500px',1);
        },
        getjqGridNetPoint: function(branchCompanyId){
            $("#jqGridNetPoint").jqGrid({
                url: baseURL + 'psnetpoint/list',
                datatype: "json",
                postData:{'branchCompanyId': branchCompanyId},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true},
                    { label: '网点名称', name: 'pointName', index: 'point_name', width: 120 },
                    { label: '负责人', name: 'person', index: 'person', width: 120 },
                    { label: '电话', name: 'telphone', index: 'telphone', width: 80 },
                    { label: '所属分公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 80 ,hidden:true},
                    { label: '所属分公司名称', name: 'branchCompany', index: 'branch_company', width: 130 ,hidden:true},
                    { label: '网点地址', name: 'address', index: 'address', width: 300 },
                    { label: '营业时间', name: 'shopHours', index: 'shop_hours', width: 80 ,hidden:true},
                    { label: '收件时间', name: 'receivingTime', index: 'receiving_time', width: 80 ,hidden:true},
                    { label: '经度', name: 'longitude', index: 'longitude', width: 80 ,hidden:true},
                    { label: '纬度', name: 'latitude', index: 'latitude', width: 80 ,hidden:true},
                    { label: '省', name: 'province', index: 'province', width: 80,hidden:true },
                    { label: '市', name: 'city', index: 'city', width: 80 ,hidden:true},
                    { label: '区', name: 'country', index: 'country', width: 80 ,hidden:true},
                    { label: '高德云图id(高德检索用)', name: 'gaoId', index: 'gao_id', width: 80 ,hidden:true}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 25,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerNetPoint",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridNetPoint").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        }
	}
});

function dateChange(dateTime){
    if(dateTime != null) {
        var seperator1 = "-";
        var year = dateTime.getFullYear();
        var month = dateTime.getMonth() + 1;
        var strDate = dateTime.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }else{
        return null;
    }
};

/** 分公司弹窗确定和关闭按钮处理 **/
$("#addBranchCompany").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridBranchCompany").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择分公司!");
        return ;
    }
    var rowData = $("#jqGridBranchCompany").jqGrid('getRowData',id);
    vm.psOrder.branchCompany = rowData.companyName;
    vm.psOrder.branchCompanyId = rowData.id;
    vm.q.branchCompany = rowData.companyName;

    utils.closeCurrentPage();
    vm.getjqGridNetPoint(vm.psOrder.branchCompanyId);
});

$("#closeBranchCompany").click(function(){
    utils.closeCurrentPage();
});

/** 网点弹窗口确定和关闭按钮处理 **/
$("#addNetPoint").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridNetPoint").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择网点!");
        return ;
    }
    var rowData = $("#jqGridNetPoint").jqGrid('getRowData',id);
    vm.psOrder.netPoint = rowData.pointName;
    vm.psOrder.netPointId = rowData.id;
    vm.q.netPoint = rowData.pointName;

    utils.closeCurrentPage();
});

$("#closeNetPoint").click(function(){
    utils.closeCurrentPage();
});